package cn.huobi.org.controller;

import cn.huobi.basic.util.AjaxResult;
import cn.huobi.basic.util.PageList;
import cn.huobi.org.domain.Detail;
import cn.huobi.org.domain.Type;
import cn.huobi.org.query.TypeQuery;
import cn.huobi.org.service.DetailService;
import cn.huobi.org.service.TypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/type")
public class TypeController {

    @Autowired
    private TypeService typeService;

    @Autowired
    private DetailService detailService;

    /**
     * 查询所有
     */
    @GetMapping
    public List<Type> findAll(){
        return typeService.findAll();
    }

    /**
     * 查询单条
     */
    @GetMapping("/{id}")
    public Type findById(@PathVariable("id") Long id){
        return typeService.findById(id);
    }

    /**
     * 添加或修改
     */
    @PutMapping
    public AjaxResult add(@RequestBody Type type){
        if (type.getId() == null){
            // 添加
            typeService.add(type);
        }else {
            // 修改
            typeService.update(type);
        }
        return AjaxResult.me().success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public AjaxResult detele(@PathVariable("id") Long id){
        List<Detail> byTypesId = detailService.findByTypesId(id);
        for (Detail detail : byTypesId) {
            detailService.delete(detail.getId());
        }
        typeService.delete(id);
        return AjaxResult.me().success();
    }

    /**
     * 分页查询
     */
    @PostMapping
    public PageList<Type> queryPage(@RequestBody TypeQuery query){
        return typeService.queryByPage(query);
    }

    /**
     *  批量删除
     */
    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        typeService.batchDel(ids);
        return AjaxResult.me().success();
    }

    /**
     * 联级删除
     */



}
